/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoPubmed;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoPubmedService;

import java.util.List;

/**
 * 考研信息管理Controller
 *
 * @author yt
 * @version 2017-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoPubmed")
public class CampusinfoPubmedController extends BaseController {

    @Autowired
    private CampusinfoPubmedService campusinfoPubmedService;

    @Autowired
    private CampusinfoPayService campusinfoPayService;

    @ModelAttribute
    public CampusinfoPubmed get(@RequestParam(required = false) String id) {
        CampusinfoPubmed entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoPubmedService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoPubmed();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoeducation:campusinfoPubmed:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoPubmed campusinfoPubmed, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoPubmed> page = campusinfoPubmedService.findPage(new Page<CampusinfoPubmed>(request, response), campusinfoPubmed);
        List<CampusinfoPubmed> campusinfoPubmeds = campusinfoPubmedService.findList(campusinfoPubmed);
        model.addAttribute("campusinfoPubmeds", campusinfoPubmeds);
        model.addAttribute("page", page);
        return "modules/campusinfoeducation/campusinfoPubmedList";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoPubmed:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoPubmed campusinfoPubmed, Model model) {
        model.addAttribute("campusinfoPubmed", campusinfoPubmed);
        return "modules/campusinfoeducation/campusinfoPubmedForm";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoPubmed:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoPubmed campusinfoPubmed, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoPubmed)) {
            return form(campusinfoPubmed, model);
        }
        List<CampusinfoPubmed> campusinfoPubmeds= campusinfoPubmedService.findList(campusinfoPubmed);
        campusinfoPubmedService.save(campusinfoPubmed);
        addMessage(redirectAttributes, "保存考研信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoPubmed/?repage";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoPubmed:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoPubmed campusinfoPubmed, RedirectAttributes redirectAttributes) {
        campusinfoPubmedService.delete(campusinfoPubmed);
        addMessage(redirectAttributes, "删除考研信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoPubmed/?repage";
    }
    @RequiresPermissions("campusinfoeducation:campusinfoPubmed:edit")
    @RequestMapping(value = "status")
    public String status(CampusinfoPubmed campusinfoPubmed, RedirectAttributes redirectAttributes) {
        campusinfoPubmed.setStatus(CampusinfoOrderStat.DONE.getValue());
        campusinfoPubmedService.status(campusinfoPubmed);
        CampusinfoPay campusinfoPay = new CampusinfoPay();
        campusinfoPay.setId(campusinfoPubmed.getOrderCode());
        campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
        campusinfoPayService.status(campusinfoPay);
        addMessage(redirectAttributes, "修改订单状态");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoPubmed/?repage";
    }

}